package cn.itcast.service;

import cn.itcast.interfaces.CheckGroupService;
import cn.itcast.mapper.CheckGroupMapper;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.PageResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.reactivex.Observable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/13 16:46
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //添加检查组数据
        checkGroupMapper.add(checkGroup);

        //添加检查组与检查项关联
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    public void setCheckGroupAndCheckItem(Integer checkgroupId,Integer[] checkitemIds){
        if(!ObjectUtils.isEmpty(checkitemIds) && checkitemIds.length > 0){
            List<Map> list = new ArrayList<>();
            Map map = null;
            /*for (Integer checkitemId : checkitemIds) {
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                list.add(map);
            }*/
            //Rxjava遍历
            Observable.fromArray(checkitemIds).subscribe(checkitemId -> {
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                list.add(map);
            });
            checkGroupMapper.setCheckGroupAndCheckItem(list);
        }
    }

    /**
     * 查询检查组分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page page =checkGroupMapper.selecByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据检查组id获取检查组数据
     * @param id
     * @return
     */
    @Override
    public CheckGroup findCheckgroupById(Integer id) {
        return checkGroupMapper.findCheckgroupById(id);

    }

    /**
     * 根据检查组id获取关联的检查项id集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //解除检查组与原来检查项之间的关联
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //关联检查组id与提交的检查项id
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        //更新检查组的信息
        checkGroupMapper.edit(checkGroup);
    }

    /**
     * 根据id删除检查组
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //解除检查组与原检查项的关联
        checkGroupMapper.deleteAssociation(id);
        //删除检查组
        checkGroupMapper.delete(id);
    }

    /**
     * 获取所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
      return checkGroupMapper.findAll();
    }
}
